package com.yetm.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {
        //目标对象
        Target target = new Target();

        //增强对象
        Adivce adivce = new Adivce();

        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //前置增强
                adivce.before();

                //执行目标方法
                Object invoke = method.invoke(target, args);

                //后置增强
                adivce.after();

                return invoke;
            }
        });

        proxy.show();
    }
}
